package com.register;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.login.UserInfo;

import net.sf.json.JSONObject;

public class RegisterServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		boolean flag=true;
		JSONObject message=new JSONObject();
		String uname=request.getParameter("username");
		System.out.println(uname);
		String password1=request.getParameter("userpassword");
		String password2=request.getParameter("userpassword2");
		String email=request.getParameter("useremail");
		if(uname==null||uname.equals("")){
			message.put("message", "用户名不得为空!");
			flag=false;
		}
		if(password1==null||password1.equals("")){
			message.put("message", "密码不得为空!");
			flag=false;
		}
		if(password2==null||password2.equals("")){
			message.put("message", "密码不得为空!");
			flag=false;
		}
		if(email==null||email.equals("")){
			message.put("message", "邮箱不得为空!");
			flag=false;
		}
		if(!password1.equals(password2)){
			message.put("message", "邮箱两次密码输入不一致！请重新输入!");
			flag=false;
		}
		if(flag){
			UserInfo user=new UserInfo();
			user.setEmail(email);
			user.setName(uname);
			user.setPassword(password1);
			RegisterImpl ri=new RegisterImpl();
			if(ri.register(user)){
				message.put("message", "注册成功！欢迎来到兼职乐！");
			}
			else{
				message.put("message", "ע注册失败！抱歉");
			}
		}
		response.getWriter().print(message);  
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
